﻿// CDialogXmlViewer.cpp: 实现文件
//

#include "pch.h"
#include "XmlMFCParse.h"
#include "afxdialogex.h"
#include "CDialogXmlViewer.h"
#include "Common.h"
#include "tinyxml2.h"
#include <fstream>
#include <set>
#include "CViewTree.h"
#define IDC_LOAD_BTN 100
// CDialogXmlViewer 对话框
extern ofstream out;
IMPLEMENT_DYNAMIC(CDialogXmlViewer, CDialogEx)
cXmlParaCraft* CDialogXmlViewer::QueryA_ByName(string name)
{
	if (m_vXmlRecordA) {
		vector<cXmlParaCraft*>* vXmlRecordA = m_vXmlRecordA->getvParaCraft();
		if (vXmlRecordA) {
			for (int i = 0; i < vXmlRecordA->size(); i++)
			{
				if (vXmlRecordA->at(i)->GetChineseName()._Equal(name))
				{
					return(vXmlRecordA->at(i));
				}
			}
		}
	}
	return nullptr;
}
cXmlParaCraft* CDialogXmlViewer::QueryB_ByName(string name)
{
	if (m_vXmlRecordB) {
		vector<cXmlParaCraft*>* vXmlRecordB = m_vXmlRecordB->getvParaCraft();
		if (vXmlRecordB) {
			for (int i = 0; i < vXmlRecordB->size(); i++)
			{
				if (vXmlRecordB->at(i)->GetChineseName()._Equal(name))
				{
					return(vXmlRecordB->at(i));
				}
			}
		}
	}
	return nullptr;
}
CDialogXmlViewer::CDialogXmlViewer(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_XMLVIEWER, pParent)
{
	m_vXmlRecordA=nullptr;
	m_vXmlRecordB=nullptr;
	m_vLoadList = nullptr;
}
CDialogXmlViewer::CDialogXmlViewer(CWnd* pParent, cXmlParaDoc* vXmlRecordA, cXmlParaDoc* vXmlRecordB,vector<string>* vLoadList)
	: CDialogEx(IDD_DIALOG_XMLVIEWER, pParent)
{
	m_vXmlRecordA = vXmlRecordA;
	m_vXmlRecordB = vXmlRecordB;
	m_vLoadList = vLoadList;
}
CDialogXmlViewer::~CDialogXmlViewer()
{
}

void CDialogXmlViewer::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	//DDX_Control(pDX, IDC_LIST_SHOW, m_XmlViewList);
	DDX_Control(pDX, IDC_TREE_XML_SHOW, m_XmlTree);
	DDX_Control(pDX, IDC_LIST_VALUE, m_XmlValueList);
	DDX_Control(pDX, IDC_CHECK_IMPORTANTCRAFT, m_CheckImportantCraft);
}


BEGIN_MESSAGE_MAP(CDialogXmlViewer, CDialogEx)
	ON_WM_CREATE()
	//ON_BN_CLICKED(IDC_BUTTON_EXPAND, &CDialogXmlViewer::OnBnClickedButtonExpand)
	//ON_BN_CLICKED(IDC_BUTTON_COLLAPSE, &CDialogXmlViewer::OnBnClickedButtonCollapse)
	ON_BN_CLICKED(IDC_BUTTON_SAVEA, &CDialogXmlViewer::OnBnClickedButtonSavea)
	ON_BN_CLICKED(IDC_BUTTON_SAVEB, &CDialogXmlViewer::OnBnClickedButtonSaveb)
	ON_BN_CLICKED(IDC_BUTTON_LOAD_A, &CDialogXmlViewer::OnBnClickedButtonLoadA)
	ON_BN_CLICKED(IDC_BUTTON_LOAD_B, &CDialogXmlViewer::OnBnClickedButtonLoadB)
	ON_BN_CLICKED(IDC_BUTTON_UNLOAD_A, &CDialogXmlViewer::OnBnClickedButtonUnloadA)
	ON_BN_CLICKED(IDC_BUTTON_UNLOAD_B, &CDialogXmlViewer::OnBnClickedButtonUnloadB)
	//ON_BN_CLICKED(IDC_BUTTON_SELCRAFT, &CDialogXmlViewer::OnBnClickedButtonSelcraft)
	ON_BN_CLICKED(IDC_CHECK_IMPORTANTCRAFT, &CDialogXmlViewer::OnBnClickedCheckImportantcraft)
END_MESSAGE_MAP()

// CDialogXmlViewer 消息处理程序

BOOL CDialogXmlViewer::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	//树形控件对部分节点隐藏复选框必须在init中添加这两句
	m_XmlTree.ModifyStyle(TVS_CHECKBOXES| TVS_HASBUTTONS, 0);
	m_XmlTree.ModifyStyle(0, TVS_CHECKBOXES | TVS_HASBUTTONS);

	m_XmlValueList.EnableTips(true);
	m_XmlValueList.RegisterHeadCtrl();
	m_XmlTree.SetCurDoc(m_vXmlRecordA, m_vXmlRecordB);
	m_XmlTree.SetClistCtrl(&m_XmlValueList);
	CFont* m_Font = m_XmlValueList.GetFont();
	LOGFONT LogFont;
	m_Font->GetLogFont(&LogFont);
	LogFont.lfHeight = 20;//设置字体大小
	m_Font->Detach();
	m_Font->CreateFontIndirect(&LogFont);
	m_XmlValueList.SetFont(m_Font);
	CFont* m_TreeFont = m_XmlTree.GetFont();
	LOGFONT TreeLogFont;
	m_TreeFont->GetLogFont(&TreeLogFont);
	TreeLogFont.lfHeight = 20;//设置字体大小
	m_TreeFont->Detach();
	m_TreeFont->CreateFontIndirect(&TreeLogFont);
	m_XmlTree.SetFont(m_TreeFont);
	ShowDataCraft();
	CenterWindow(NULL);
	CFont m_fnt;
	m_fnt.CreateFont(20, 10, 10, 10, FW_NORMAL, FALSE, FALSE, 0,
		ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
		DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, _T("黑体"));
	CWnd*  pWnd = GetDlgItem(IDC_BUTTON_SAVEA);
	pWnd->SetFont(&m_fnt);
	pWnd = GetDlgItem(IDC_BUTTON_SAVEB);
	pWnd->SetFont(&m_fnt);
	pWnd = GetDlgItem(IDC_BUTTON_UNLOAD_A);
	pWnd->SetFont(&m_fnt);
	pWnd = GetDlgItem(IDC_BUTTON_UNLOAD_B);
	pWnd->SetFont(&m_fnt);
	pWnd = GetDlgItem(IDC_BUTTON_LOAD_A);
	pWnd->SetFont(&m_fnt);
	pWnd = GetDlgItem(IDC_BUTTON_LOAD_B);
	pWnd->SetFont(&m_fnt);
	pWnd = GetDlgItem(IDC_BUTTON_LOAD_B);
	pWnd->SetFont(&m_fnt);
	pWnd = GetDlgItem(IDC_CHECK_IMPORTANTCRAFT);
	pWnd->SetFont(&m_fnt);
	//pWnd = GetDlgItem(IDOK);
	//pWnd->SetFont(&m_fnt);
	//pWnd = GetDlgItem(IDCANCEL);
	//pWnd->SetFont(&m_fnt);
	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}
void CDialogXmlViewer::InsertCraftByName(string name)
{
	cXmlParaCraft* craft_A = QueryA_ByName(name);
	cXmlParaCraft* craft_B = QueryB_ByName(name);
	HTREEITEM Grades = m_XmlTree.InsertItemPara(ConvertUTF8ToCString(name), 0, 0, nullptr);
	ShowDataParaCraftGroup(Grades, craft_A, craft_B);
	if (craft_A == nullptr || craft_B == nullptr)
	{
		m_XmlTree.SetGradesDiff(Grades);
	}
}
void CDialogXmlViewer::ShowDataCraft()
{
	//先插入craft,建立两个craft的并集
	set<string> s_Craft;
	vector<string> v_Craft;
	vector<cXmlParaCraft*>* vParaCraftA = nullptr;
	vector<cXmlParaCraft*>* vParaCraftB = nullptr;
	if (m_vXmlRecordA)
		vParaCraftA = m_vXmlRecordA->getvParaCraft();
	if (m_vXmlRecordB)
		vParaCraftB = m_vXmlRecordB->getvParaCraft();
	if (vParaCraftA) {
		for (auto it : *(vParaCraftA))
		{
			s_Craft.insert(it->GetChineseName());
			v_Craft.push_back(it->GetChineseName());
		}
	}
	if (vParaCraftB) {
		for (auto it : *(vParaCraftB))
		{
			if (s_Craft.find(it->GetChineseName()) == s_Craft.end())//不存在往vecotor里push
			{
				v_Craft.push_back(it->GetChineseName());
			}
			s_Craft.insert(it->GetChineseName());
		}
	}
	set<string>::iterator iter = s_Craft.find(_UnicodeToUtf8(_T("切割")));
	if (iter !=s_Craft.end())
	{
		InsertCraftByName(*iter);
		s_Craft.erase(*iter);
	}
	iter = s_Craft.find(_UnicodeToUtf8(_T("穿孔")));
	if (iter != s_Craft.end())
	{
		InsertCraftByName(*iter);
		s_Craft.erase(*iter);
	}
	iter = s_Craft.find(_UnicodeToUtf8(_T("高级")));
	if (iter != s_Craft.end())
	{
		InsertCraftByName(*iter);
		s_Craft.erase(*iter);
	}
	iter = s_Craft.find(_UnicodeToUtf8(_T("轨迹")));
	if (iter != s_Craft.end())
	{
		InsertCraftByName(*iter);
		s_Craft.erase(*iter);
	}
	for (auto it : v_Craft)
	{
		if (it == _UnicodeToUtf8(_T("轨迹")) ||
			it == _UnicodeToUtf8(_T("切割")) ||
			it == _UnicodeToUtf8(_T("穿孔")) ||
			it == _UnicodeToUtf8(_T("高级"))
			)
			continue;
		InsertCraftByName(it);
	}

}
void CDialogXmlViewer::ShowDataParaCraftGroup(HTREEITEM Grades, cXmlParaCraft* craft_A, cXmlParaCraft* craft_B)
{
	set<string> s_Craft;
	vector<string> v_Craft;
	if (craft_A)
	{
		for (auto it : craft_A->GetVparaGroup())
		{
			s_Craft.insert(it->GetChineseName());
			v_Craft.push_back(it->GetChineseName());
		}
	}
	if (craft_B)
	{
		for (auto it : craft_B->GetVparaGroup())
		{
			if (s_Craft.find(it->GetChineseName()) == s_Craft.end())//不存在往vecotor里push
			{
				v_Craft.push_back(it->GetChineseName());
			}
			s_Craft.insert(it->GetChineseName());
		}
	}
	for (auto it : v_Craft)
	{
		cXmlParaGroup* group_A = nullptr;
		cXmlParaGroup* group_B = nullptr;
		HTREEITEM Grades_Group = m_XmlTree.InsertItemPara(ConvertUTF8ToCString(it), 0, 0, Grades);
		if (craft_A != nullptr)
		{
			group_A= craft_A->GetParaGroupByCName(it);
		}
		if (craft_B != nullptr)
		{
			group_B = craft_B->GetParaGroupByCName(it);
		}
		m_XmlTree.SetDataGroup(Grades_Group, group_A, group_B);
		//ShowDataParaGroupData(Grades_Group, group_A, group_B);
	}
}
int CDialogXmlViewer::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CDialogEx::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  在此添加您专用的创建代码

	return 0;
}

void CDialogXmlViewer::OnBnClickedButtonExpand()
{
	// TODO: 在此添加控件通知处理程序代码
	m_XmlTree.SetRedraw(false);
	HTREEITEM item= m_XmlTree.GetRootItem();
	while (item != nullptr) {
		m_XmlTree.ExpandTree(item);
		item = m_XmlTree.GetNextSiblingItem(item);
	}
	m_XmlTree.SetRedraw(true);
	m_XmlTree.RedrawWindow();
}


void CDialogXmlViewer::OnBnClickedButtonCollapse()
{
	m_XmlTree.SetRedraw(false);
	HTREEITEM item = m_XmlTree.GetRootItem();
	// TODO: 在此添加控件通知处理程序代码
	while (item != nullptr) {
		m_XmlTree.CollapseTree(item);
		item = m_XmlTree.GetNextSiblingItem(item);
	}
	m_XmlTree.SetRedraw(true);
	m_XmlTree.RedrawWindow();
	//m_XmlValueList.InitList();
}

void CDialogXmlViewer::OnBnClickedButtonSavea()
{
	// TODO: 在此添加控件通知处理程序代码
	if (m_vXmlRecordA)
	{
		m_vXmlRecordA->SaveFile();
	}
}

void CDialogXmlViewer::OnBnClickedButtonSaveb()
{
	// TODO: 在此添加控件通知处理程序代码
	if (m_vXmlRecordB)
	{
		m_vXmlRecordB->SaveFile();
	}
}
int CDialogXmlViewer::LoadCraftList(int flag)
{
	if (!m_vLoadList)
	{
#ifdef DEBUG
		out << "m_vLoadList is null" << endl;
#endif
		return -1;
	}
	tinyxml2::XMLDocument* loadDocument= new tinyxml2::XMLDocument();
	if (!loadDocument)
	{
#ifdef DEBUG
		out << "loadDocument is null" << endl;
#endif
		return -1;
	}
	tinyxml2::XMLDeclaration* declaration = loadDocument->NewDeclaration();
	loadDocument->InsertFirstChild(declaration);
	// 插入根节点
	tinyxml2::XMLElement* rootNode = loadDocument->NewElement("Root");
	if (!rootNode)
	{
#ifdef DEBUG
		out << "rootNode is null" << endl;
#endif
		delete loadDocument;
		return -1;
	}
	CString craftList = _T("");
	for (auto it : *m_vLoadList)
	{
		craftList += ConvertUTF8ToCString(it);
		craftList += _T(" ");
	}
	CString showMsg = _T("是否加载以下工艺 ") + craftList+_T(" 从文件");
	CString fileName = _T(" ");
	if (flag == 1)
	{
		fileName += CString(m_vXmlRecordA->GetDocDir().c_str());
	}
	if (flag == 2)
	{
		fileName += CString(m_vXmlRecordB->GetDocDir().c_str());
	}
	showMsg += fileName;
	UINT selflag = MessageBox(showMsg, TEXT("加载提示"), MB_YESNO | MB_ICONQUESTION);
	if (selflag == IDNO) {
		delete loadDocument;
		return -1;
	}
	loadDocument->InsertEndChild(rootNode);
	for (auto it : *m_vLoadList)
	{
		cXmlParaCraft* xmlCraft = nullptr;
		if (flag == 1)
			xmlCraft = QueryA_ByName(it);
		if(flag==2)
			xmlCraft = QueryB_ByName(it);
		if (xmlCraft)
		{
			tinyxml2::XMLElement* node = xmlCraft->GetXmlElement();
			if (node) {
#ifdef DEBUG
				out << "copy node "<< xmlCraft->GetChineseName() << endl;
#endif
				tinyxml2::XMLNode* copy = node->DeepClone(loadDocument);
				rootNode->InsertEndChild(copy);
			}
		}
	}
	loadDocument->SaveFile("load.xml");
	delete loadDocument;
	return 0;
}
void CDialogXmlViewer::OnBnClickedButtonLoadA()
{
	// TODO: 在此添加控件通知处理程序代码
	if (m_vLoadList)
	{
		m_vLoadList->clear();
		HTREEITEM hRootItem = m_XmlTree.GetRootItem();
		while (hRootItem != nullptr) {
			if (m_XmlTree.GetCheck(hRootItem))
			{
				CString cName = m_XmlTree.GetItemText(hRootItem);

				m_vLoadList->push_back(_UnicodeToUtf8(cName));
			}
			hRootItem = m_XmlTree.GetNextSiblingItem(hRootItem);
		}
		if (m_vLoadList->size() == 0)
		{
			AfxMessageBox(_T("未选择工艺参数"));
			return;
		}
	}
	
	int ret=LoadCraftList(1);
	if(!ret)
		EndDialog(IDC_LOAD_BTN);
}
void CDialogXmlViewer::OnBnClickedButtonLoadB()
{
	// TODO: 在此添加控件通知处理程序代码
	if (m_vLoadList)
	{
		m_vLoadList->clear();
		HTREEITEM hRootItem = m_XmlTree.GetRootItem();
		while (hRootItem != nullptr) {
			if (m_XmlTree.GetCheck(hRootItem))
			{
				CString cName = m_XmlTree.GetItemText(hRootItem);

				m_vLoadList->push_back(_UnicodeToUtf8(cName));
			}
			hRootItem = m_XmlTree.GetNextSiblingItem(hRootItem);
		}
		if (m_vLoadList->size() == 0)
		{
			AfxMessageBox(_T("未选择工艺参数"));
			return;
		}
	}
	int ret=LoadCraftList(2);
	if (!ret)
		EndDialog(IDC_LOAD_BTN);
}

void CDialogXmlViewer::OnBnClickedButtonUnloadA()
{
	// TODO: 在此添加控件通知处理程序代码
	this->SetRedraw(false);
	if (m_vXmlRecordA)
		m_vXmlRecordA->Reload();
	m_XmlTree.initParaView();
	m_XmlTree.SetCurDoc(m_vXmlRecordA, m_vXmlRecordB);
	m_XmlTree.SetClistCtrl(&m_XmlValueList);
	ShowDataCraft();
	m_XmlTree.RestoreSatuation();
	this->SetRedraw(true);
	this->RedrawWindow();

}

void CDialogXmlViewer::OnBnClickedButtonUnloadB()
{
	// TODO: 在此添加控件通知处理程序代码
	this->SetRedraw(false);
	if (m_vXmlRecordB)
		m_vXmlRecordB->Reload();
	m_XmlTree.initParaView();
	m_XmlTree.SetCurDoc(m_vXmlRecordA, m_vXmlRecordB);
	m_XmlTree.SetClistCtrl(&m_XmlValueList);
	ShowDataCraft();
	m_XmlTree.RestoreSatuation();
	this->SetRedraw(true);
	this->RedrawWindow();
}
void CDialogXmlViewer::OnBnClickedButtonSelcraft()
{
	// TODO: 在此添加控件通知处理程序代码
	m_XmlTree.SelImportantCraft();
}

BOOL CDialogXmlViewer::PreTranslateMessage(MSG* pMsg)
{
	// TODO: 在此添加专用代码和/或调用基类
	//屏蔽ESC键按下
	if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE)
	{
		return TRUE;
	}

	//屏蔽Enter键按下
	if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN && pMsg->wParam)
	{
		return TRUE;
	}
	return CDialogEx::PreTranslateMessage(pMsg);
}

void CDialogXmlViewer::OnBnClickedCheckImportantcraft()
{
	// TODO: 在此添加控件通知处理程序代码
	if (m_CheckImportantCraft.GetCheck() == true)
	{
		m_XmlTree.SelImportantCraft();
	}
	else
	{
		m_XmlTree.UnselImportantCraft();
	}
}
